/*
 *  Copyright © 2018 - 2021 xulianqiang90@163.com. All Rights Reserved.
 */

package io.light.frame.dal.mybatis.generator.core.meta.entity;

import com.alibaba.fastjson.annotation.JSONField;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

/**
 * Table column metadata
 *
 * @author Ivan
 * @version 1.0.0
 * @date 2021-05-15 07:16
 */
@Getter
@Setter
@ToString
public class TableColumn implements Metadata{

    /**
     * Database name
     */
    @JSONField(name = "table_cat")
    private String database;

    /**
     * Table name
     */
    @JSONField(name = "table_name")
    private String table;

    /**
     * Column name
     */
    @JSONField(name = "column_name")
    private String name;

    /**
     * Type name
     */
    @JSONField(name = "type_name")
    private String typeName;

    /**
     * Type code
     */
    @JSONField(name = "data_type")
    private Integer typeCode;

    /**
     * Column position
     */
    @JSONField(name = "ordinal_position")
    private Integer position;

    /**
     * Column Length
     */
    @JSONField(name = "column_size")
    private Integer length;

    /**
     * Is nullable or not
     */
    @JSONField(name = "is_nullable")
    private Boolean nullable;

    /**
     * Is auto increment or not
     */
    @JSONField(name = "is_autoincrement")
    private boolean autoIncrement;

    /**
     * Numeric scale
     */
    @JSONField(name = "decimal_digits")
    private Integer numScale;

    /**
     * Default value
     */
    @JSONField(name = "column_def")
    private Object defaultValue;

    /**
     * Column comment
     */
    @JSONField(name = "remarks")
    private String comment;

    /**
     * Is primary key or not
     */
    private boolean primaryKey;

    /**
     * Can ignore on update
     */
    private boolean canIgnoreOnUpdate;

    /**
     * Can ignore on insert
     */
    private boolean canIgnoreOnInsert;

    /**
     * Extra info
     */
    private String extra;

    /**
     * Determine metadata is valid or not
     * @return {@code boolean}
     */
    @Override
    public boolean isValid() {
        return StringUtils.isNotBlank(database) && StringUtils.isNotBlank(table) && StringUtils.isNotBlank(name)
                && StringUtils.isNotBlank(typeName) && typeCode != null;
    }
}
